import 'dart:async';

import 'package:colin_flutter/colin_flutter.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertz/components/a_button/index.dart';
import 'package:fluttertz/utils/global.dart';
import 'package:wifi_configuration/wifi_configuration.dart';

class Xzwifi extends StatefulWidget {
  Xzwifi({Key key}) : super(key: key);

  _XzwifiState createState() => _XzwifiState();
}

class _XzwifiState extends State<Xzwifi> {
  @override
  void initState() {
    super.initState();

    G.user.selectWifi = "";
    _timer = Timer.periodic(Duration(seconds: 1), (timer) async {
      getconnectedToWifi();
      if (_yanshi > 0) {
        _yanshi++;
      }
    });
  }

  void getconnectedToWifi() async {
    String selectWifi = await WifiConfiguration.connectedToWifi(); //当前连接的wifi
    if (!_isjl) {
      _selectWifi = selectWifi;
    } else {
      _jtWifi = selectWifi;
      if ((_jtWifi.contains("AI-THINKER") || _jtWifi.contains("DF-COMPANY") || _jtWifi.contains("AI-COMPANY") || _jtWifi.contains("DF-THINKER")) && _yanshi == 0) {
        _yanshi = 1;
      }
    }
    setState(() {});
  }

  Timer _timer;
  bool _isjl = false; //是否已经记录WiFi
  String _selectWifi = "";
  String _jtWifi = "";
  int _yanshi = 0;

  @override
  void dispose() {
    _timer?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage("lib/assets/images/tz/bg.png"),
              fit: BoxFit.cover,
            )),
        child: Scaffold(
          resizeToAvoidBottomInset: false,
          backgroundColor: Colors.transparent,
          appBar: customAppbar(context: context, title: '步驟 1'),
          body: Container(
            color: Colors.transparent,
            padding: EdgeInsets.only(left: 35, right: 35, top: 1),
            child: Column(
              children: <Widget>[
                Container(
                    margin: EdgeInsets.symmetric(vertical: 20, horizontal: 10),
                    // color: rgba(136, 175, 213, 1),
                    width: double.infinity,
                    child: Text(
                      "確認 APP 讀取顯示是您的場內 Wi-Fi 後，點擊 紀錄 Wi-Fi;台主小幫手雲端裝置僅能支援 2.4G 無線網路，請確 認您當前連接的是 2.4G 無線網路",
                      style: TextStyle(fontSize: 16),
                    )),
                Container(
                    margin: EdgeInsets.symmetric(vertical: 20, horizontal: 10),
                    // color: rgba(136, 175, 213, 1),
                    width: double.infinity,
                    child: Text(
                      "Wi-Fi: $_selectWifi",
                      style: TextStyle(fontSize: 16),
                    )),
                Container(
                    margin: EdgeInsets.only(
                        left: MediaQuery.of(context).size.width - 200),
                    width: 120,
                    child: AButton.normal(
                      child: Text('記錄Wi-Fi',
                          style: TextStyle(
                              fontSize: 18, fontWeight: FontWeight.bold)),
                      onPressed: () {
                        G.user.selectWifi = _selectWifi;
                        G.toast('已記錄！');
                        _isjl = true;
                        setState(() {});
                      },
                    )),
                Container(
                  margin: EdgeInsets.only(top:20, bottom: 20),
                  width: 180,
                  height: 94,
                  child: Image.asset(
                    'lib/assets/images/tz/bg_wifi02.png',
                    fit: BoxFit.cover,
                  ),
                ),
                Container(
                    child: Text(
                      "遠端消保器盡可能靠近路由器",
                      style: TextStyle(fontSize: 16),
                    )),
                Container(
                    child: Text(
                      "連接裝置:AI-THINKER...or DF-COMPANY",
                      style: TextStyle(fontSize: 16, height: 2),
                    )),
                Container(
                    margin: EdgeInsets.symmetric(vertical: 20, horizontal: 10),
                    width: double.infinity,
                    child: AButton.normal(
                        child: Text('連接裝置 Wi-Fi',
                            style: TextStyle(
                                fontSize: 18, fontWeight: FontWeight.bold)),
                        type: 'info',
                        onPressed: () {
                          openWifiSettings();
                        })),
                nextbtn(),

              ],
            ),
          ),
        ));
  }

  Future<void> openWifiSettings() async {
    String msg;
    try {
      msg = await ColinFlutter.openWifiSettings;
    } on PlatformException {
      msg = 'Failed to open WifiSettings.';
    }
  }

  Widget nextbtn() {
    if (_isjl && (_jtWifi.contains("AI-THINKER") || _jtWifi.contains("DF-COMPANY") || _jtWifi.contains("AI-COMPANY") || _jtWifi.contains("DF-THINKER")) && _yanshi > 4) {
      return Container(
          margin: EdgeInsets.symmetric(horizontal: 10),
          width: double.infinity,
          child: AButton.normal(
            child: Text('下一步',
                style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
            type: 'info',
            onPressed: () => G.pushNamed('/ljwifi'),
          ));
    } else {
      return Container(
          margin: EdgeInsets.symmetric(horizontal: 10),
          width: double.infinity,
          child: AButton.normal(
            child: Text('下一步',
                style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
            type: 'info',
          ));
    }
  }
}
